{{ template "head" . }}
<div class="layui-container" style="margin-top: 15px;">
    <form method="post" action="/publish" class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-block">
                <select name="cid" class="layui-select">
                    {{ range .category }}
                    <option value="{{ .Id }}">{{ .Title }}</option>
                    {{ end}}
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea name="content" id="content" class="layui-textarea" rows="5" placeholder="请输入内容"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">发布</button>
                <button class="layui-btn layui-btn-danger" type="reset">重置</button>
            </div>
        </div>
    </form>
</div>
{{ template "footer" . }}
<script>
    //Demo
    layui.use(['form','layedit'], function(){
        var form = layui.form;
        var layedit = layui.layedit;
        layedit.set({
            uploadImage: {
                url: '/upload' //接口url
            }
        });
        let index = layedit.build('content'); //建立编辑器
        //监听提交
        form.on('submit(submit)', function(data){
            data.field.content = layedit.getContent(index);
            $.post('/publish' , data.field , (data) => {
                if (data.status) {
                    layer.msg(data.msg)
                    return;
                }
                layer.msg(data.msg)
                setTimeout(() => {
                    window.parent.location.reload();
                } , 1500);
            } , 'json');
            return false;
        });
    });
</script>